﻿using DTOs;
using Microsoft.IdentityModel.Tokens;
using RBAC.Domain.Service.Interfae;
using RBAC.Repository.Interfae;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RBAC.Domain.Service.Impl
{
    public class LeaveService : ILeaveService
    {
        /// <summary>
        /// 用户仓储
        /// </summary>
        private readonly IUserRepository _userRepository;
        /// <summary>
        /// 角色仓储
        /// </summary>
        private readonly IRoleRepository _roleRepository;
        /// <summary>
        /// 请假仓储
        /// </summary>
        private readonly ILeaveRepository _leaveRepository;
        /// <summary>
        /// 用户与角色仓储
        /// </summary>
        private readonly IUserRoleRepository _userRoleRepository;
       public LeaveService(IUserRepository userRepository, IRoleRepository roleRepository, 
           ILeaveRepository leaveRepository, IUserRoleRepository userRoleRepository)
        {
            _userRepository = userRepository;
            _roleRepository = roleRepository;
            _leaveRepository = leaveRepository;
            _userRoleRepository = userRoleRepository;
        }
        /// <summary>
        /// 添加请假信息
        /// </summary>
        /// <param name="leave"></param>
        /// <returns></returns>
        public async Task<int> AddLeave(Leave leave)
        {
            return await _leaveRepository.AddAsync(leave);
        }
        /// <summary>
        /// 删除请假
        /// </summary>
        /// <param name="leaveId"></param>
        /// <returns></returns>
        public async Task<int> DelLeave(int leaveId)
        {
            var dels = await _leaveRepository.GetModelsAsync(leaveId);
            dels.LeaveDel = true;
            return await _leaveRepository.UpdateAsync(dels);
        }

        public async Task<int> DelLeave(Leave leave)
        {
            var dels = await _leaveRepository.GetModelsAsync(leave.LeaveId);
            dels.LeaveDel = true;
            return await _leaveRepository.UpdateAsync(dels);
        }

        /// <summary>
        /// 反填请假信息
        /// </summary>
        /// <param name="leaveId"></param>
        /// <returns></returns>
        public async Task<Leave> GetLeaveById(int leaveId)
        {
            return await _leaveRepository.GetModelsAsync(leaveId);
        }
        /// <summary>
        /// 显示请假信息
        /// </summary>
        /// <returns></returns>
        public async Task<PageDto<Leave>> GetLeavesList( string names,int pageSize, int pageIndex, string? Sdate, string? Edate, string? Lnames)
        {
            PageDto<Leave> pageDto= new PageDto<Leave>();

            var listobj = from a in await _leaveRepository.GetAllAsync()
                          where !a.LeaveDel && a.LeaveName == names||a.Rolenames==names
                          select a;
            if(!string.IsNullOrEmpty(Sdate))
            {
                listobj = listobj.Where(d => d.LeaveSdate>=DateTime.Parse(Sdate));
            }
            if (!string.IsNullOrEmpty(Edate))
            {
                listobj = listobj.Where(d => d.LeaveSdate <= DateTime.Parse(Edate));
            }
            if (!string.IsNullOrEmpty(Lnames))
            {
                listobj = listobj.Where(d => d.LeaveName.Contains(Lnames));
            }
            pageDto.ToalCount = listobj.Count();
            pageDto.PageCount = (int)Math.Ceiling(listobj.Count() * 1.0 / pageSize);
            pageDto.list = listobj.OrderByDescending(d => d.addDate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return pageDto;
            
        }

        /// <summary>
        /// 修改请假信息
        /// </summary>
        /// <param name="leave"></param>
        /// <returns></returns>
        public async Task<int> UpdateLeave(Leave leave)
        {
            return await _leaveRepository.UpdateAsync(leave);
        }
        /// <summary>
        /// 审核修改状态
        /// </summary>
        /// <param name="lid"></param>
        /// <param name="sart"></param>
        /// <returns></returns>
        public async Task<int> UpdateSart(int lid, bool sart, string? approseIsno)
        {
           var ss=await _leaveRepository.GetModelsAsync(lid);
            ss.Sart = sart;
            ss.ApproseIsno = approseIsno;
            return await _leaveRepository.UpdateAsync(ss);
        }
    }
}
